package com.guanghua.brick.html.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.html.ICell;
import com.guanghua.brick.html.IComponent;
import com.guanghua.brick.html.ILayout;

public class ContainFlowLayoutTag extends BodyTagSupport implements IComponent, ILayout {
	
	private static Log logger = LogFactory.getLog(ContainFlowLayoutTag.class);
	
	private String id = null;
	private String name = null;
	private String html = "";
	private int columns = 1;
	private List<ICell> list = new ArrayList<ICell>();
	private boolean keepCell = true;

	public int doEndTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			out.println(buildLayout());
		} catch (IOException e) {
			logger.error("io exception on print flow layout html", e);
		}
		return BodyTagSupport.EVAL_PAGE;
	}
	
	private String buildLayout() {
		StringBuffer html = new StringBuffer();
		html.append("<table ").append(this.html).append(">\n");
		html.append("<tr>\n");
		
		int c = this.columns;
		int a = 0;
		Iterator<ICell> i = this.list.iterator();
		while (i.hasNext()) {
			
			ICell cell = i.next();
			//所占的列为0或者负，就不显示
			if (cell.getCols() > 0) {
				//所占的列小于该行所剩的列
				if (cell.getCols() <= c) {
					html.append("<td");
					if (cell.getCols() != 1) {
						html.append(" ")
							.append(cell.getHtml())
							.append(" colspan=\"")
							.append(cell.getCols())
							.append("\"");
					}
					html.append(">\n");
					html.append(cell.getBody())
						.append("\n");
					html.append("</td>\n");
					
					//减去占用的单元
					c -= cell.getCols();
					
				//所占的列大于该行所剩的列
				} else {
					if (this.keepCell) {
						for (int j = 0; j < c; j ++) {
							html.append("<td>&nbsp;</td>\n");
						}
					} else {
						html.append("<td colspan=\"")
							.append(c)
							.append("\">\n");
						html.append(cell.getBody());
						html.append("</td>\n");
					}
					//所剩下的单元为0
					c = 0;
				}
				a++;
			}
			
			//如果所剩下的单元为0,一行结束
			if (c == 0) {
				c = this.columns;
				//还有则，加上结束和下一个开始
				if (i.hasNext()) {
					html.append("</tr>\n");
					html.append("<tr>\n");
				}
			}
		}
		
		//结束还剩余cell
		if (c != this.columns) {
			for (int j = 0; j < c; j ++) {
				html.append("<td>&nbsp;</td>\n");
			}
		//刚好结束，或者没有一个cell
		} else {
			//没有一个显示的cell
			if (a == 0) {
				html.append("<td>&nbsp;</td>\n");
			}
		}
		
		html.append("</tr>\n");
		html.append("</table>\n");
		return html.toString();
	}
	
	public void appendCell(ICell cell) {
		list.add(cell);
	}
	public int getColumns() {
		return columns;
	}
	public void setColumns(int columns) {
		this.columns = columns;
	}
	public String getHtml() {
		return html;
	}
	public void setHtml(String html) {
		this.html = html;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public boolean isKeepCell() {
		return keepCell;
	}

	public void setKeepCell(boolean keepCell) {
		this.keepCell = keepCell;
	}
	
//	public static void main(String[] a) {
//		ContainFlowLayoutTag c = new ContainFlowLayoutTag();
//		for (int i = 0 ; i < 11; i ++) {
//			ICell cell = new CommonCell();
//			cell.setBody("xxxxxx" + i);
//			//cell.setCols(i%3);
//			c.appendCell(cell);
//		}
//		c.setColumns(4);
//		logger.info(c.buildLayout());
//	}
}
